package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.utils.DateUtils;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbCommodityService;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.entity.TbOrderEntity;
import com.ge.modules.entity.TbStoreEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 店铺订单
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-11 17:28:43
 */
@RestController
@RequestMapping("/api/store/order")
public class TbStoreOrderController {
    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 显示订单列表(可根据不同状态显示不同订单)
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
        if (tbStoreEntity == null) {
            return R.error(501, "登录失效");
        }
        params.put("commodityMasterStoreId", tbStoreEntity.getStoreId());
        //查询列表数据
        Query query = new Query(params);
        List<TbOrderEntity> tbOrderEntityList = tbOrderService.queryList(query);
        int total = tbOrderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbOrderEntityList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询订单详细信息
     */
    @RequestMapping("/info/{orderCode}")
    public R info(@LoginStoreApi TbStoreEntity tbStoreEntity, @PathVariable("orderCode") String orderCode) {
        if (tbStoreEntity == null) {
            return R.error(501, "登录失效");
        }
        try {
            //通过对象存储条件 根据订单号和用户查询
            TbOrderEntity tbOrderEntity = new TbOrderEntity();
            tbOrderEntity.setOrderCode(orderCode);
            tbOrderEntity.setCommodityMasterStoreId(tbStoreEntity.getStoreId());
            TbOrderEntity tbOrderEntity1 = tbOrderService.queryObject(tbOrderEntity);
            return R.ok().put("data", tbOrderEntity1);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 修改订单状态
     *
     * @param tbStoreEntity
     * @param tbOrderEntity
     * @return
     */
    @RequestMapping("/update")
    public R update(@LoginStoreApi TbStoreEntity tbStoreEntity, TbOrderEntity tbOrderEntity) {
        if (tbStoreEntity == null) {
            return R.error(501, "登录失效");
        }
        try {
            //如果是选择接单 就修改为 待使用状态
            if (tbOrderEntity.getOrderAcceptStatus() == 1) {
                tbOrderEntity.setOrderStatus(4);
            }
            tbOrderEntity.setCommodityMasterStoreId(tbStoreEntity.getStoreId());
            tbOrderEntity.setAcceptAndRefuseOrderTime(new Date());
            tbOrderService.update(tbOrderEntity);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 批量删除 做状态修改
     *
     * @param orderCodes 订单编号数组
     * @return
     */
    @RequestMapping("/deleteList")
    public R deleteList(@RequestBody Integer[] orderCodes) {
        tbOrderService.updateBatch(orderCodes);
        return R.ok();
    }

    /**
     * 根据时间统计 订单数量和总价
     *
     * @param tbStoreEntity
     * @param timeSign
     * @return COUNT(id) orders,SUM(order_price) totalPrice
     */
    @GetMapping("/countOrders")
    public R countOrders(@LoginStoreApi TbStoreEntity tbStoreEntity, String timeSign) {
        Map<String, Object> map = new HashMap<>();
        map.put("commodityMasterStoreId", tbStoreEntity.getStoreId());
        //统计已完成的订单 5
        map.put("orderStatus", 5);
        //判断时间标识是不否是查询当天订单的数据 等0就添加时间查询 1则不添加
        timeSign = "2";
        if (!timeSign.equals("1")) {
            //得到年月日的时间字符串
            String dates = DateUtils.dateToString(new Date(), DateUtils.DATE_PATTERN);
            //在拼接一天的开始时间和结束时间
            String startDates = dates + " 00:00:00";
            String endDates = dates + " 23:59:59";
            map.put("startTime", DateUtils.toDate(startDates, DateUtils.DATE_TIME_PATTERN));
            map.put("endTime", DateUtils.toDate(endDates, DateUtils.DATE_TIME_PATTERN));
        }
        Map m = tbOrderService.queryOrdersAndTotal(map);
        return R.ok().put("data", m);
    }

    /**
     * 根据二维码修改订单状态 （即订单号）
     *
     * @param tbStoreEntity
     * @param scanCode
     * @return
     */
    @PostMapping("/updateOrderByScanCode")
    public R updateOrderByScanCode(@LoginStoreApi TbStoreEntity tbStoreEntity, String scanCode) {
        TbOrderEntity tbOrderEntity = new TbOrderEntity();
        tbOrderEntity.setOrderCode(scanCode);
        TbOrderEntity orderEntity = tbOrderService.queryObject(tbOrderEntity);
        if(orderEntity == null){
            return R.error("未找到订单");
        }
        //判断该订单是否是当前店铺的
        if(tbStoreEntity.getStoreId() != orderEntity.getCommodityMasterStoreId()){
            return R.error("未知订单 订单不属于当前店铺的");
        }
        //设置为已完成
        orderEntity.setOrderStatus(5);
        orderEntity.setOrderAcceptStatus(3);
        orderEntity.setUpdateTime(new Date());
        tbOrderService.update(orderEntity);
        return R.ok().put("data",orderEntity);
    }

    /**
     * 根据密码修改订单状态
     *
     * @param tbStoreEntity
     * @param scanCode
     * @return
     */
    @PostMapping("updateOrderByPassWord")
    public R updateOrderByPassWord(@LoginStoreApi TbStoreEntity tbStoreEntity, String scanCode){
        //

        return R.ok();
    }

}
